package com.example.as_sum.car;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.as_sum.R;
import com.example.as_sum.entity.ShopInfo;
import com.example.as_sum.event.ShopCarItemEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;


/**
 * Created by ran on 2016/8/16.
 */
public class ShopCarAdapter extends RecyclerView.Adapter<ShopCarAdapter.MyViewHoder>{
    private List<ShopInfo> shopInfos;
    private Context context;
    public ShopCarAdapter(Context context,List<ShopInfo> shopInfos) {
        this.context=context;
        this.shopInfos=shopInfos;
    }
    public void update(List<ShopInfo> allCourses){
        this.shopInfos=allCourses;
        notifyDataSetChanged();//刷新ui
    }
    @Override
    public int getItemCount() {
        return shopInfos.size();
    }

    @Override
    public MyViewHoder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHoder holder = new MyViewHoder(LayoutInflater.from(
                context).inflate(R.layout.shopcar_item, parent,
                false));
        return holder;
    }
    @Override
    public void onBindViewHolder(final MyViewHoder holder, int position) {
        final ShopInfo shopInfo=shopInfos.get(position);
        holder.tv_info.setText(shopInfo.getSinfo());
        holder.tv_price.setText(shopInfo.getPrice()+"$");
        if(shopInfo.isState()){
            holder.img_check.setChecked(true);
        }else{
            holder.img_check.setChecked(false);
        }
        holder.img_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(holder.img_check.isChecked()){
                    shopInfo.setState(true);
                    EventBus.getDefault().post(new ShopCarItemEvent(shopInfo.getSid(),true,shopInfo.getPrice(),shopInfo.getCount()));
                }else{
                    shopInfo.setState(false);
                    EventBus.getDefault().post(new ShopCarItemEvent(shopInfo.getSid(),false,shopInfo.getPrice(),shopInfo.getCount()));
                }
            }
        });
    }
    class MyViewHoder extends RecyclerView.ViewHolder{
        TextView tv_price,tv_info;
        CheckBox img_check;
        public MyViewHoder(View itemView) {
            super(itemView);
            tv_price=(TextView)itemView.findViewById(R.id.item_shop_price);
            tv_info=(TextView)itemView.findViewById(R.id.item_shop_info);
            img_check=(CheckBox) itemView.findViewById(R.id.item_img_check);
        }
    }
}
